package com.xinli.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.xinli.business.service.IFtMemberService;
import com.xinli.common.core.domain.LoginPhonePassWordBody;
import com.xinli.common.core.domain.model.BdPhoneBody;
import com.xinli.common.core.domain.model.ForgetPassWordBody;
import com.xinli.common.core.domain.model.LoginXcxBody;
import com.xinli.common.exception.ServiceException;
import com.xinli.common.utils.StringUtils;
import com.xinli.common.utils.YzCodeUtils;
import com.xinli.framework.web.service.SysRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.xinli.common.constant.Constants;
import com.xinli.common.core.domain.AjaxResult;
import com.xinli.common.core.domain.entity.SysMenu;
import com.xinli.common.core.domain.entity.SysUser;
import com.xinli.common.core.domain.model.LoginBody;
import com.xinli.common.utils.SecurityUtils;
import com.xinli.framework.web.service.SysLoginService;
import com.xinli.framework.web.service.SysPermissionService;
import com.xinli.system.service.ISysMenuService;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;


    @Autowired
    private IFtMemberService ftMemberService;

    @Autowired
    private SysRegisterService registerService;
    @Autowired
    private YzCodeUtils  yzCodeUtils;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 登录--------方法
     */
    @PostMapping("/loginXcx")
    public AjaxResult loginXcx(@RequestBody LoginXcxBody loginBody) throws Exception {
        // 生成令牌
        HashMap<String, String> hashMap = loginService.loginXcx(loginBody);
        return AjaxResult.success(hashMap);
    }

    /**
     * 登录--------手机号和密码
     */
    @PostMapping("/loginByPhone")
    public AjaxResult loginByPhone(@RequestBody LoginPhonePassWordBody passWordBody) throws Exception {
        // 生成令牌
        String s = loginService.loginByPhone(passWordBody.getPhone(), passWordBody.getPassword());
        return AjaxResult.success(s);
    }




    /**
     * 绑定手机号
     */
    @PostMapping("/bdPhone")
    public AjaxResult bdPhone(@RequestBody BdPhoneBody bdPhoneBody) throws Exception {
        HashMap<String, String> hashMap = new HashMap<>();
        String verycode_msg = yzCodeUtils.checkRedisKey(bdPhoneBody.getPhone(), bdPhoneBody.getCode());
        if (!StringUtils.isEmpty(verycode_msg)) {
            throw  new ServiceException("验证码错误");
        }
        // 绑定手机号
        registerService.bdPhone(bdPhoneBody);
        String token = loginService.loginByUnionId(bdPhoneBody.getUnionId());
        hashMap.put("token", token);
        // 生成令牌
        return AjaxResult.success(hashMap);
    }


    /**
     * 忘记密码
     */

    @PostMapping("/forgetPassword")
    public AjaxResult forgetPassword(@RequestBody ForgetPassWordBody forgetPassWordBody) throws Exception{
        String verycode_msg = yzCodeUtils.checkRedisKey(forgetPassWordBody.getPhone(), forgetPassWordBody.getCode());
        if (!StringUtils.isEmpty(verycode_msg)) {
            throw  new ServiceException("验证码错误");
        }
        registerService.resetPassWord(forgetPassWordBody);
        return AjaxResult.success();
    }


    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
